﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;
using System.Diagnostics;

namespace DAL
{
    public class StationRepository
    {
        // 添加工站
        public int AddStation(Station station)
        {
            string sql = @"INSERT INTO station 
                            (TotalProcessId, StationName, StationVersion, StationDescription, 
                             StationStepNumber, StationType, Area, ReworkStatus) 
                           VALUES 
                            (@TotalProcessId, @StationName, @StationVersion, @StationDescription, 
                             @StationStepNumber, @StationType, @Area, @ReworkStatus)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@TotalProcessId", station.TotalProcessId),
                new MySqlParameter("@StationName", station.StationName),
                new MySqlParameter("@StationVersion", station.StationVersion),
                new MySqlParameter("@StationDescription", station.StationDescription),
                new MySqlParameter("@StationStepNumber", station.StationStepNumber),
                new MySqlParameter("@StationType", station.StationType),
                new MySqlParameter("@Area", station.Area),
                new MySqlParameter("@ReworkStatus", station.ReworkStatus)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除工站
        public int DeleteStation(int stationId)
        {
            string sql = "DELETE FROM station WHERE StationId = @StationId";
            MySqlParameter[] parameters = { new MySqlParameter("@StationId", stationId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新工站信息
        public int UpdateStation(Station station)
        {
            string sql = @"UPDATE station 
                            SET TotalProcessId = @TotalProcessId, 
                                StationName = @StationName, 
                                StationVersion = @StationVersion, 
                                StationDescription = @StationDescription, 
                                StationStepNumber = @StationStepNumber, 
                                StationType = @StationType, 
                                Area = @Area, 
                                ReworkStatus = @ReworkStatus 
                          WHERE StationId = @StationId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@TotalProcessId", station.TotalProcessId),
                new MySqlParameter("@StationName", station.StationName),
                new MySqlParameter("@StationVersion", station.StationVersion),
                new MySqlParameter("@StationDescription", station.StationDescription),
                new MySqlParameter("@StationStepNumber", station.StationStepNumber),
                new MySqlParameter("@StationType", station.StationType),
                new MySqlParameter("@Area", station.Area),
                new MySqlParameter("@ReworkStatus", station.ReworkStatus),
                new MySqlParameter("@StationId", station.StationId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有工站
        public List<Station> GetAllStations()
        {
            string sql = "SELECT * FROM station";
            return DBHelper.ExecuteQuery(sql, null, MapStation);
        }

        // 查询单个工站
        public Station GetStationById(int stationId)
        {
            string sql = "SELECT * FROM station WHERE StationId = @StationId";
            MySqlParameter[] parameters = { new MySqlParameter("@StationId", stationId) };
            var stations = DBHelper.ExecuteQuery(sql, parameters, MapStation);
            return stations.FirstOrDefault();
        }

        // 查询所有totolId相同工站
        public List<Station> GetStationByTotolId(int totolId)
        {
            string sql = "SELECT * FROM station WHERE TotalProcessId = @TotalProcessId";
            MySqlParameter[] parameters = { new MySqlParameter("@TotalProcessId", totolId) };
            List<Station> stations = DBHelper.ExecuteQuery(sql, parameters, MapStation);
            foreach(Station station in stations)
            {
                Debug.WriteLine("totalid相同的工站", station.StationId);
            }
            return stations;
        }
        // 根据总流程 ID 和步骤序号获取工站 ID
        public int GetStationIdByTotalProcessIdAndStepNumber(int totalProcessId, int stepNumber)
        {
            string sql = @"SELECT StationId FROM station 
                           WHERE TotalProcessId = @TotalProcessId 
                           AND StationStepNumber = @StepNumber";

            MySqlParameter[] parameters = {
                new MySqlParameter("@TotalProcessId", totalProcessId),
                new MySqlParameter("@StepNumber", stepNumber)
            };

            var result = DBHelper.ExecuteScalar(sql, parameters);
            return result != null ? Convert.ToInt32(result) : -1; // 返回-1表示未找到
        }

        // 映射数据库查询结果到 Station 对象
        public Station MapStation(MySqlDataReader reader)
        {
            return new Station
            {
                StationId = (int)reader["StationId"],
                TotalProcessId = (int)reader["TotalProcessId"],
                StationName = reader["StationName"].ToString(),
                StationVersion = reader["StationVersion"].ToString(),
                StationDescription = reader["StationDescription"] is DBNull ? null : reader["StationDescription"].ToString(),
                StationStepNumber = (int)reader["StationStepNumber"],
                StationType = reader["StationType"].ToString(),
                Area = reader["Area"].ToString(),
                ReworkStatus = reader["ReworkStatus"].ToString()
            };
        }
    }
}